﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NAccounting.QueryObjects
{
    public class ProjectActivityQueryObject : QueryObject<ProjectActivity>
    {
        public ProjectActivityQueryObject(NAccountEntities context)
            : base(context)
        { }

        public int? ProjectId { get; set; }
        public bool? Active { get; set; }

        public override PagedResult<ProjectActivity> Get(int pageIndex, int rows)
        {
            var result = new PagedResult<ProjectActivity>();
            result.Total = Context.ProjectActivities
                                  .Where(Condition)
                                  .Count();

            result.List = Context.ProjectActivities
                                 .Where(Condition)
                                 .OrderBy(x => x.ExpectedToStart)
                                 .Skip(pageIndex * rows)
                                 .Take(rows).ToArray();
            return result;
        }

        public override IEnumerable<ProjectActivity> Get()
        {
            var result = Context.ProjectActivities
                                 .Where(Condition)
                                 .OrderBy(x => x.ExpectedToStart)
                                 .ToArray();
            return result;
        }

        protected override bool Condition(ProjectActivity entity)
        {
            return (!ProjectId.HasValue || entity.ProjectId == ProjectId.Value) &&
                   (!Active.HasValue || entity.IsActive == Active.Value);
        }
    }
}